fn main() {
    let res = Solution::longest_palindrome("babad".to_string());
    println!("{}", res);
}

struct Solution;

impl Solution {
    pub fn longest_palindrome(s: String) -> String {
        let len = s.len();
        let mut max = 0;
        let mut res = String::new();
        for i in 0..len {
            for j in (i+1..=len).rev() {
                let sublen = j - i;
                if max >= sublen {
                    break;
                }
                let substr = &s[i..j];
                if Solution::is_palindromic_string(substr.to_string()) {
                    max = sublen;
                    res = substr.to_string();
                    break;
                }
            }
        }
        res
    }

    fn is_palindromic_string(s: String) -> bool {
        let len = s.chars().count() / 2;
        let mut chars1 = s.chars();
        let mut chars2 = s.chars().rev();
        for _i in 0..len {
            if let Some(char1) = chars1.next() {
                if let Some(char2) = chars2.next() {
                    if char1 != char2 {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
